package cn.adkii.springframework.test.bean;

import cn.adkii.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * @author malingdi
 * @description
 * @date 2023/11/27 18:38
 */
public class ProxyBeanFactory implements FactoryBean<IUserDao> {

    @Override
    public IUserDao getObject() throws Exception {
        InvocationHandler handler = (proxy, method, args) ->{
          Map<String, String> hashMap = new HashMap<>();
          hashMap.put("10001", "adkii");
          hashMap.put("10002", "小马");
          hashMap.put("10003", "小张");
          return "你被代理了" + method.getName() + ":" + hashMap.get(args[0].toString());
        };
        return (IUserDao) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{IUserDao.class}, handler);
    }

    @Override
    public Class<?> getObjectType() {
        return IUserDao.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
